package org.linlinjava.litemall.wx.aiweb;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.linlinjava.litemall.core.util.DateTimeUtil;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.LemonlaActor;
import org.linlinjava.litemall.db.domain.LemonlaSession;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.service.LemonlaActorService;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("/wx/ai/actor")
@RestController
public class WxAiActorController {
    @Autowired
    private LemonlaActorService actorService;

    @Autowired
    private LitemallCollectService collectService;

    @PostMapping("/get")
    public Object getOne(@LoginUser Integer userId, @RequestBody String body){
        Integer id = JacksonUtil.parseInteger(body, "id");
        LemonlaActor actor = actorService.findById(id);
        if (userId!=null) {
            LitemallCollect litemallCollect = collectService.queryByTypeAndValue(userId, (byte) 0, actor.getId());
            if (litemallCollect!=null) {
                actor.setStatus((byte) 1);
            }
        }
        return ResponseUtil.ok(actor);
    }

    @PostMapping("/delete")
    public Object delete(@LoginUser Integer userId, @RequestBody String body){
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Integer id = JacksonUtil.parseInteger(body, "id");
        LemonlaActor actor = actorService.findById(id);
        if (actor==null) {
            return ResponseUtil.ok("未找到应用");
        }
        if (userId.equals(actor.getCreateUserId())) {
            actorService.deleteById(id);
        }
        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object create(@LoginUser Integer userId,@RequestBody LemonlaActor actor) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        actor.setStatus((byte) 1);
        actor.setCreateUserId(userId);
        if (actor.getName()==null) {
            actor.setName(actor.getNickname()+ DateTimeUtil.getDateShortString(new Date()));
        }
        actorService.add(actor);
        return ResponseUtil.ok(actor);
    }

    @PostMapping("/getMine")
    public Object getMine(@LoginUser Integer userId){
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<LemonlaActor> actor = actorService.findbyUserId(userId);
        return ResponseUtil.ok(actor);
    }
}
